package cn.shenlong.util;

import javax.servlet.http.HttpServletRequest;

/**
 * 验证码工具类
 * 
 * @author shenlong
 */
public class CaptchaUtil {

	/**
	 * 生成校验码并设置到Session中
	 * 
	 * @param request
	 * @param seed
	 *            种子
	 */
	public static String generateCaptchaCode(HttpServletRequest request,
			String seed) {
		int randomCode = (int) (Math.random() * 900000) + 100000;
		String randomCodeSession = String.valueOf(randomCode);
		request.getSession().setAttribute(seed, randomCodeSession);
		return String.valueOf(randomCode);
	}

	/**
	 * @param request
	 * @param seed
	 * @param userCapatcha
	 *            用户输入的校验码
	 * @return
	 */
	public static boolean checkCaptchaCode(HttpServletRequest request,
			String seed, String userCapatcha) {
		Object obj = request.getSession().getAttribute(seed);
		String captacha = obj != null ? obj.toString() : "";
		return captacha.equals(userCapatcha) ? true : false;
	}
}
